package com.jason.kotlin.meta.architecture1.ui

import android.os.Bundle
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.jason.kotlin.meta.R
import com.jason.kotlin.meta.architecture1.adapter.MetaImageAdapter
import com.jason.library.common.ext.observe
import com.jason.library.common.widget.swipetoloadlayout.OnLoadMoreListener
import com.jason.library.common.widget.swipetoloadlayout.OnRefreshListener
import com.jason.library.common.widget.swipetoloadlayout.SwipeToLoadLayout

class MetaImageActivity : AppCompatActivity(), OnLoadMoreListener, OnRefreshListener {

    companion object {
        const val DEFAULT_WORD = "美女"
    }

    private lateinit var swipeToLoadLayout: SwipeToLoadLayout
    private lateinit var recyclerView: RecyclerView
    private lateinit var keywordEdit: EditText
    private lateinit var searchBtn: Button
    private lateinit var changeBtn: Button

    private lateinit var imageAdapter: MetaImageAdapter

    private lateinit var viewModel: MetaImageViewModel

    private var currentKeyWord =
        DEFAULT_WORD


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_meta_image)

        initView()

        initListener()


        viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(
            MetaImageViewModel::class.java)
        //viewModel = ViewModelProviders.of(this).get(MetaImageViewModel::class.java)

        bindLiveData()

        swipeToLoadLayout.postDelayed({ swipeToLoadLayout.isRefreshing = true }, 100)

    }

    /**
     * 初始化试图控件
     */
    private fun initView() {
        swipeToLoadLayout = findViewById(R.id.swipeToLoadLayout)
        recyclerView = findViewById(R.id.swipe_target)
        keywordEdit = findViewById(R.id.edit_keyword)
        searchBtn = findViewById(R.id.btn_search)
        changeBtn = findViewById(R.id.btn_change)
        keywordEdit.setText(DEFAULT_WORD)


        imageAdapter =
            MetaImageAdapter(this)

        recyclerView.apply {
            layoutManager = LinearLayoutManager(this@MetaImageActivity)
            setHasFixedSize(true)
            adapter = imageAdapter
        }
    }

    private fun initListener() {
        swipeToLoadLayout.setOnLoadMoreListener(this@MetaImageActivity)
        swipeToLoadLayout.setOnRefreshListener(this@MetaImageActivity)

        searchBtn.setOnClickListener {
            currentKeyWord = keywordEdit.text.toString()
            if (TextUtils.isEmpty(currentKeyWord)) {
                keywordEdit.error = "不能为空"
                return@setOnClickListener
            }
            viewModel.apply {
                keyword = currentKeyWord
                loadData()
            }
        }

        changeBtn.setOnClickListener {
            when (imageAdapter.currentType) {
                MetaConstant.META_IMAGE_TYPE_LINEAR -> {
                    recyclerView.layoutManager = GridLayoutManager(this@MetaImageActivity, 2)
                    imageAdapter.updateImageShowType(MetaConstant.META_IMAGE_TYPE_GRID)
                }

                MetaConstant.META_IMAGE_TYPE_GRID -> {
                    recyclerView.layoutManager =
                        StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
                    imageAdapter.updateImageShowType(MetaConstant.META_IMAGE_TYPE_STAGE)
                }

                MetaConstant.META_IMAGE_TYPE_STAGE -> {
                    recyclerView.layoutManager = LinearLayoutManager(this@MetaImageActivity)
                    imageAdapter.updateImageShowType(MetaConstant.META_IMAGE_TYPE_LINEAR)
                }
            }
        }
    }

    private fun bindLiveData() {

        viewModel.keyword =
            DEFAULT_WORD

        observe(viewModel.data) {
            if(it.page > 0) {
                imageAdapter.addData(it.data)
            }else{
                imageAdapter.setData(it.data)
            }

            swipeToLoadLayout.isRefreshing = false
            swipeToLoadLayout.isLoadingMore = false
        }
    }

    override fun onLoadMore() {
        viewModel.loadData()
    }

    override fun onRefresh() {
        viewModel.loadMore()
    }

}